package com.wzb.eye100.activity;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.wzb.eye100.R;
import com.wzb.eye100.interf.WApplication;
import com.wzb.eye100.util.Common;
import com.wzb.eye100.util.ExcelUtil;
import com.wzb.eye100.util.LogUtil;
import com.wzb.eye100.util.ToastUtil;
import com.wzb.eye100.view.MyGridAdapter;
import com.wzb.eye100.view.MyGridView;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * @author wzb<wangzhibin_x@qq.com>
 * @date May 3, 2017 10:55:01 AM
 */
public class MainActivity extends BaseActivity implements OnClickListener {

	private MyGridView mGridView;

	private ImageView backView;

	private TextView titleView;


	private String[] img_text;
	private int[] img_icon = { R.drawable.menu_eye, R.drawable.menu_setting, R.drawable.menu_file,
				R.drawable.menu_photo,};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
		setContentView(R.layout.activity_main);

		initView();
	}

	private void initView() {
		Resources res = getResources();
		img_text = res.getStringArray(R.array.img_text);

		mGridView = findViewById(R.id.gridview);
		mGridView.setOnItemClickListener(new MyItemClickListener());

		backView = findViewById(R.id.title_back);
		backView.setOnClickListener(this);
		backView.setVisibility(View.GONE);

		titleView = findViewById(R.id.title_text);
		titleView.setText(getString(R.string.home_page));
		titleView.setTextSize(22);

	}

	@Override
	protected void onResume() {
		super.onResume();
		mGridView.setAdapter(new MyGridAdapter(this, img_text, img_icon));
	}

	private class MyItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			switch (arg2) {

				case 0:
					break;
				case 1:
					break;
				case 2:
					openApp("com.mediatek.filemanager","com.mediatek.filemanager.FileManagerOperationActivity");
					break;
				case 3:
					openApp("com.android.gallery3d","com.android.gallery3d.app.GalleryActivity");
					break;
			default:
				break;
			}

		}

	}



	private void openApp(String pkg,String activityName)
	{
		Intent intent=new Intent();
		intent.setClassName(pkg,activityName);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		try{
			startActivity(intent);
		}catch (Exception e){
			LogUtil.logMessage("wzb","openApp err:"+e.toString());
		}

	}
	

	private long firstTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			long secondTime = System.currentTimeMillis();
			if (secondTime - firstTime > 2000) {// 如果两次按键时间间隔大于2秒，则不退出
				Toast.makeText(this, getString(R.string.exit_dialog), Toast.LENGTH_LONG).show();
				firstTime = secondTime;// 更新firstTime
				return true;
			} else {// 两次按键小于2秒时，退出应用
				exit();
				return false;
			}
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	private void exit() {
		for (Activity activity : WApplication.activityList) {
			activity.finish();
		}
		finish();
		System.exit(0);
		System.gc();
	}

}
